package com.zwface.demo.function.main.mvvm;


import android.content.Context;
import android.content.DialogInterface;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.zwface.demo.App;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseViewModel;
import com.zwface.demo.function.main.event.SelectCoursesEvent;
import com.zwface.demo.greendao.CourseManager;
import com.zwface.demo.greendao.JoinUserWithCourseManager;
import com.zwface.demo.model.db.Course;
import com.zwface.demo.model.db.JoinUserWithCourse;
import com.zwface.demo.model.db.UserInfo;
import com.zwface.demo.utils.DialogHelper;
import com.zwface.demo.utils.FastjsonHelper;
import com.zwface.demo.utils.SharedPreferenceUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * created by      张中文
 * on:             2020/4/28 16:37
 * 描述：
 */
public class MainViewModel extends BaseViewModel
{
    public ObservableField<String> userName = new ObservableField();
    public ObservableField<String> stuNo = new ObservableField();
    public ObservableField<String> face = new ObservableField();
    public ObservableField<String> tyepName = new ObservableField();

    public void getUserInfo(){
        String uName = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_NAME);
        String uNo = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_STUNO);
        String uFace = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_FACE);
        userName.set(uName);
        stuNo.set(uNo);
        face.set(uFace);
        tyepName.set(isTeacher.get()?"教师：":"学生：");

    }

    /**
     * 显示所有课程弹窗
     */
    public void showAllCourseDlg(Context context){
        List<Course> dbList = CourseManager.queryAllCourse();
        if(dbList == null || dbList.size() == 0)
            return;
        final String[] items = new String[dbList.size()];
        for (int i=0;i<dbList.size();i++) {
            items[i] = dbList.get(i).getCourseName();
        }
        final QMUIDialog.MultiCheckableDialogBuilder builder = new QMUIDialog.MultiCheckableDialogBuilder(context)
                //.setCheckedItems(new int[]{1, 3})
                .setSkinManager(QMUISkinManager.defaultInstance(context))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                //清除关联表中此用户下的所有课程
                String userId = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_ID);
                JoinUserWithCourseManager.deleteJoin(Long.parseLong(userId));

                List<Course> selectLs = new ArrayList<>();
                for (int i = 0; i < builder.getCheckedItemIndexes().length; i++) {
                    int selectPos = builder.getCheckedItemIndexes()[i];

                    //保存数据库
                    saveCourse(dbList.get(selectPos));
                    selectLs.add(dbList.get(selectPos));
                }
                showSelectCourses(selectLs);
                dialog.dismiss();
            }
        });
        builder.create(DialogHelper.mCurrentDialogStyle).show();
    }

    /**
     * 显示选的课
     */
    private void showSelectCourses( List<Course> selectLs ){
        List<Course> courseModels[] = new ArrayList[7];

        for (int i = 0; i < courseModels.length; i++) {
            courseModels[i] = new ArrayList<>();
        }

        List<Course> models_1 = new ArrayList<>();
        List<Course> models_2 = new ArrayList<>();
        List<Course> models_3 = new ArrayList<>();
        List<Course> models_4 = new ArrayList<>();
        List<Course> models_5 = new ArrayList<>();
        List<Course> models_6 = new ArrayList<>();
        List<Course> models_7 = new ArrayList<>();

        for (Course course:selectLs) {
            switch (course.getWeek()){
                case 1:
                    models_1.add(course);
                    break;
                case 2:
                    models_2.add(course);
                    break;
                case 3:
                    models_3.add(course);
                    break;
                case 4:
                    models_4.add(course);
                    break;
                case 5:
                    models_5.add(course);
                    break;
                case 6:
                    models_6.add(course);
                    break;
                case 7:
                    models_7.add(course);
                    break;
            }
        }
        courseModels[0].addAll(models_1);
        courseModels[1].addAll(models_2);
        courseModels[2].addAll(models_3);
        courseModels[3].addAll(models_4);
        courseModels[4].addAll(models_5);
        courseModels[5].addAll(models_6);
        courseModels[6].addAll(models_7);

        EventBus.getDefault().post(new SelectCoursesEvent(courseModels));
    }

    private void saveCourse(Course course){
        //添加课程
        course.setCourseFlag((int) (Math.random() * 10));
        Long courseId = CourseManager.addCourse(course);
        //添加用户-课程关联信息
        String userId = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_ID);
        JoinUserWithCourse joinUserWithCourse = new JoinUserWithCourse();
        joinUserWithCourse.setCId(courseId);
        joinUserWithCourse.setUId(Long.parseLong(userId));
        JoinUserWithCourseManager.addJoinUserWithCoutse(joinUserWithCourse);
    }

    @BindingAdapter({"loadImageUrl"})
    public static void loadImageUrl(ImageView imageView, String url){
        Glide.with(imageView)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .into(imageView);
    }
}
